package hunterxhunter.greedisland.entities.text;

import hunterxhunter.greedisland.events.KeyPressedUseActionEvent;
import hunterxhunter.greedisland.events.KeyPressedEvent;
import java.awt.Color;
import java.awt.event.KeyEvent;
import framework.eea.StateBasedGame;
import framework.eea.entity.Entity;
import framework.eea.event.Component;
import framework.eea.event.Event;
import framework.eea.gui.Graphics;
import framework.eea.gui.JFrameGameContainer;
import framework.eea.interfaces.Action;

/**
 *
 * @author Markus Schr&ouml;der
 */
public class SelectionEntity extends Entity {

    private int gap;
    private String[] options;
    private int current;
    private boolean spin;
    private Color normal;
    private Color selected;
    private Action[] actions;
    
    public SelectionEntity(String id) {
        super(id);
    }
    
    public SelectionEntity(String id, int centerX, int y, int gap, boolean spin, Color normal, Color selected, String[] options, final Action[] actions) {
        super(id);
        
        getPosition().x = centerX;
        getPosition().y = y;
        this.gap = gap;
        this.spin = spin;
        this.options = options;
        this.current = 0;
        this.normal = normal;
        this.selected = selected;
        this.actions = actions;
        
        
        addComponent(new KeyPressedUseActionEvent(
                new int[] { KeyEvent.VK_DOWN, KeyEvent.VK_UP, KeyEvent.VK_ENTER }, 
                new Action[] { 
                    new Action() {
                        @Override
                        public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                            inc();
                        }
                    },
                    new Action() {
                        @Override
                        public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                            dec();
                        }
                    },
                    new Action() {
                        @Override
                        public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                            actions[current].update(gc, sb, delta, event);
                        }
                    },
                }));
    }
    
    public void reset() {
        this.current = 0;
    }
    
    public void inc() {
        current++;
        if(current >= options.length){
            if(spin)
                current = 0;
            else
                current = options.length-1;
        }
    }
    
    public void dec() {
        current--;
        if(current < 0) {
            if(spin)
                current = options.length-1;
            else
                current = 0;
        }
    }
    
    @Override
    public void render(JFrameGameContainer gc, StateBasedGame sb, Graphics g) {
        int y = getPosition().y;
        int index = 0;
        for(String option : options) {
            
            if(current == index)
                g.setColor(selected);
            else
                g.setColor(normal);
            
            g.drawStringHoriCenter(option, getPosition().x, y);
            index++;
            y += g.getFontHeight() + gap;
        }
    }
}
